package com.sunshine.health.sys.health;

import com.netflix.appinfo.HealthCheckHandler;
import com.netflix.appinfo.InstanceInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.health.Status;
import org.springframework.stereotype.Component;

/**
 * 健康检查处理器<br />
 * 本例中，eureka会每30秒执行一次getStatus()方法，把所得的服务器实例状态更新到服务器中<br />
 * 可修改配置文件中的instanceInfoReplicationIntervalSeconds数值来改变执行时间<br />
 * @author WeiHong
 * @date 2018/11/12
 */
@Component
public class MyHealthCheckHandler implements HealthCheckHandler {

    @Autowired
    private MyHealthIndicator indicator;

    @Override
    public InstanceInfo.InstanceStatus getStatus(InstanceInfo.InstanceStatus instanceStatus) {
        Status s = indicator.health().getStatus();
        if (s.equals(Status.UP)) {
            System.out.println("数据库正常连接");
            return InstanceInfo.InstanceStatus.UP;
        } else {
            System.out.println("数据库无法连接");
            return InstanceInfo.InstanceStatus.DOWN;
        }
    }
}
